/**
 * Created by Leeho on 2017/6/21.
 */
//  全局的filter

/**
 * 根据秒数返回的时间格式为 00:00
 * @time 时间
 * return string
 */
function transformTime (time) {
  if (!time) return '00:00' //  避免传入undefined引发的错误
  var min = parseInt(time / 60)
  var sec = parseInt(time % 60)
  return (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
}

// 时间戳转日期格式
function timestampFormat (time) {
  console.log(time)
  var date = new Date(time / 1000)
  var y = date.getFullYear()
  var m = date.getMonth() + 1
  m = m < 10 ? ('0' + m) : m
  var d = date.getDate()
  d = d < 10 ? ('0' + d) : d
  var h = date.getHours()
  h = h < 10 ? ('0' + h) : h
  var minute = date.getMinutes()
  var second = date.getSeconds()
  minute = minute < 10 ? ('0' + minute) : minute
  second = second < 10 ? ('0' + second) : second
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
}

// 时间戳转日期格式
function FormatDate (date, type) {
  if (!date) return ''
  date = !date ? new Date() : date
  date = typeof date === 'number' ? new Date(date) : date
  type = type || 'yyyy-MM-dd HH:mm:ss'
  let obj = {
    'y': date.getFullYear(), // 年份，注意必须用getFullYear
    'M': date.getMonth() + 1, // 月份，注意是从0-11
    'd': date.getDate(), // 日期
    'q': Math.floor((date.getMonth() + 3) / 3), // 季度
    'w': date.getDay(), // 星期，注意是0-6
    'H': date.getHours(), // 24小时制
    'h': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 12小时制
    'm': date.getMinutes(), // 分钟
    's': date.getSeconds(), // 秒
    'S': date.getMilliseconds() // 毫秒
  }
  let week = ['天', '一', '二', '三', '四', '五', '六']
  for (let i in obj) {
    type = type.replace(new RegExp(i + '+', 'g'), function (m) {
      let val = obj[i] + ''
      if (i === 'w') return (m.length > 2 ? '星期' : '周') + week[val]
      for (let j = 0, len = val.length; j < m.length - len; j++) val = '0' + val
      return m.length === 1 ? val : val.substring(val.length - m.length)
    })
  }
  return type
}

/**
 * 根据收听数量返回数字带单位
 * @num 收听量
 * return string
 */
function transformNumberCount (num) {
  if (!num) return '' //  避免传入undefined引发的错误
  if (num < 100000) { //  小于10万
    return num
  } else if (num > 100000 && num < 10000000) { //  10万到1000万，后面带个单位万
    return (num / 10000).toFixed(1) + '万'
  } else if (num > 100000000) { //  1亿以上（目前没发现有）
    return (num / 100000000).toFixed(1) + '亿'
  }
}

/**
 * 去掉前后空格
 */
function trim (value) {
  return value.trim()
}
/*
* 运动类型
*/
function sportsFormat (val) {
  switch (val) {
    case 3001: return '足球'
    case 3002: return '篮球'
    case 3003: return '羽毛球'
    case 3004: return '网球'
    case 3005: return '乒乓球'
    case 3006: return '跑步'
  }
}

/*
*赛制类型
*/
function matchType (val) {
  switch (val) {
    case 24001: return '淘汰制1场'
    case 24002: return '淘汰制1场'
    case 24003: return '淘汰制1场'
    case 25001: return '三局两胜'
    case 26001: return '15分制'
    case 26002: return '21分制'
    case 24004: return '单淘汰制'
    case 27001: return '单盘'
    case 27002: return '三盘'
    case 27003: return '五盘'
    case 25002: return '4局'
    case 25003: return '4局'
  }
}

// 赞助-招商计划 列表状态
function sponsorStatus (val) {
  switch (val) {
    case 0: return '草稿'
    case 1: return '审核中'
    case 2: return '不通过'
    case 3: return '通过'
  }
}

function sponsorBidStatus (val) {
  switch (val) {
    case 0: return '已投标'
    case 1: return '已取消'
    case 2: return '未中标'
    case 3: return '已中标'
  }
}

function positiveInt (val) {
  return val < 0 ? 0 : parseInt(val)
}
function formatMoney (num) {
  // 从后端获取的单位为分，不是元
  num = num.toString().replace(/\$|,/g, '') / 100
  if (isNaN(num)) {
    num = '0'
  }
  let sign = (Number(num) === (num = Math.abs(num)))
  num = Math.floor(num * 100 + 0.50000000001)
  let cents = num % 100
  num = Math.floor(num / 100).toString()
  if (cents < 10) {
    cents = '0' + cents
  }
  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3))
  }
  return '￥' + (((sign) ? '' : '-') + num + '.' + cents)
}
function groupName (name) {
  switch (name) {
    case 'all': return '全赛程'
    case 'knockout': return '淘汰赛'
    default: return name + '组'
  }
}

function NumberToArab (num) {
  let list = []
  for (let i = 0; i < 26; i++) {
    list.push(String.fromCharCode(65 + i) + '组')
  }
  for (let j = 0; j < (32 - 26); j++) {
    list.push('A' + String.fromCharCode(65 + j + 1) + '组')
  }
  list.push('淘汰赛')
  return list[num]
}
function getWeekList (weekStr) { // code数组转周几
  let weekArr = weekStr.split(',').sort()
  let weekDayArr = []
  for (let weekDay of weekArr) {
    switch (weekDay) {
      case '1':
        weekDayArr.push('一')
        break
      case '2':
        weekDayArr.push('二')
        break
      case '3':
        weekDayArr.push('三')
        break
      case '4':
        weekDayArr.push('四')
        break
      case '5':
        weekDayArr.push('五')
        break
      case '6':
        weekDayArr.push('六')
        break
      case '7':
        weekDayArr.push('日')
        break
    }
  }
  return '每周' + weekDayArr.join('、')
}
function getWeekDay (weekCode) { // code转周几
  let weekDay = ''
  switch (weekCode) {
    case 1:
      weekDay = '周一'
      break
    case 2:
      weekDay = '周二'
      break
    case 3:
      weekDay = '周三'
      break
    case 4:
      weekDay = '周四'
      break
    case 5:
      weekDay = '周五'
      break
    case 6:
      weekDay = '周六'
      break
    case 7:
      weekDay = '周日'
      break
  }
  return weekDay
}
export default {
  transformTime: transformTime,
  transformNumberCount: transformNumberCount,
  timestampFormat: timestampFormat,
  sportsFormat: sportsFormat,
  matchType: matchType,
  positiveInt: positiveInt,
  sponsorStatus: sponsorStatus,
  FormatDate: FormatDate,
  trim: trim,
  formatMoney,
  groupName,
  NumberToArab,
  getWeekList,
  getWeekDay,
  sponsorBidStatus
}
